package com.tgpms.vforms.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.tgpms.common.Result;
import com.tgpms.exception.ExceptionExplain;
import com.tgpms.interfaces.dto.VformInterface;
import com.tgpms.util.Common;
import com.tgpms.util.PageInfo;
import com.tgpms.util.VformBeanUtil;
import com.tgpms.vforms.entity.Company;
import com.tgpms.vforms.service.CompanyService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * Created By Riven on 2020-8-10
 */
@Service
@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, rollbackFor = Exception.class)
public class CompanyServiceImpl implements CompanyService {

    @Resource
    private VformInterface vformInterface;

    @Override
    public Result selCompanyList(PageInfo pageInfo) {
        JSONObject param = new JSONObject();
        Object selparam = pageInfo.getQueryMap().get("selparam");
        Object leftdition = pageInfo.getQueryMap().get("leftdition");
        Object condition = pageInfo.getQueryMap().get("condition");
        String dataowner = "";
        if (pageInfo.getQueryMap().get("dataowner") != null)
            dataowner = pageInfo.getQueryMap().get("dataowner").toString();
        if(Common.isEmpty(selparam)) {
            param.put("[SQL1]", " ");
        }else {
            param.put("[SQL1]", selparam.toString());
        }
        if(Common.isEmpty(leftdition)) {
            param.put("[SQL2]", " ");
        }else {
            param.put("[SQL2]", leftdition.toString());
        }
        if(Common.isEmpty(condition)) {
            param.put("[SQL3]", " ");
        }else {
            param.put("[SQL3]", condition.toString());
        }
        if (StringUtils.isEmpty(dataowner))
            return vformInterface.multiForMsQueryBySql("SBMXT001", param.toJSONString(), pageInfo.getPage(),
                pageInfo.getSize(), "N", "N", "");
        else
            return vformInterface.multiForMsQueryBySql("SBMXT001", param.toJSONString(), pageInfo.getPage(),
                    pageInfo.getSize(), "Y", "Y", dataowner.toString());
    }

    @Override
    public Map<String, String> addCompanyInfo(Company company) {
        Map<String, String> map = new HashMap<String, String>();
        //对象转json字符串, 字段驼峰转下划线大写字段
        JSONObject json = VformBeanUtil.entityToJsonString(company);
        if (json.isEmpty()) {
            map.put("code", "-2");
            map.put("info", ExceptionExplain.ERROR_BY_PARSING.getExplain());
            return map;
        }
        //vform同步插入
        Result result = vformInterface.insert("D4010", json);
//		System.out.println("vform增加返回结果   "+result);
        if (result == null || !result.getSuccess()) {
            map.put("code", "-3");
            map.put("info", ExceptionExplain.ERROR_BY_VFORM_POST.getExplain());
            return map;
        }
        String resData = result.getData().toString();
        if (resData.equals("{\"message\":\"login\"}")) {
            map.put("code", "-3");
            map.put("info", ExceptionExplain.ERROR_BY_OVERDUE.getExplain());
            return map;
        }
        JSONObject reJson = JSONObject.parseObject(resData);
        if (reJson.get("status").toString().equals("error")) {
            map.put("code", "-4");
            map.put("info", reJson.get("validateError").toString());
            return map;
        } else {
            map.put("code", "1");
            map.put("info", ExceptionExplain.SUCCESS_BY_INSERT.getExplain());
        }
        return map;
    }

    @Override
    public Map<String, String> updateCompanyInfo(Company company) {
        Map<String, String> map = new HashMap<String, String>();
        JSONObject keyjson = new JSONObject();
        keyjson.put("COMPANY", company.getCompany());
        JSONObject pajson = VformBeanUtil.entityToJsonStringByUpdate(company);
        pajson.remove("COMPANY");

//        System.out.println("vform更新上送主键   "+keyjson.toString());
//        System.out.println("vform更新上送参数   "+pajson.toString());
        //vform同步更新
        Result result = vformInterface.update("D4010", keyjson.toString(), pajson.toString(), null, null, null);
//        System.out.println("vform更新返回结果   "+result);
        if (result == null || !result.getSuccess()) {
            map.put("code", "-3");
            map.put("info", ExceptionExplain.ERROR_BY_VFORM_POST.getExplain());
            return map;
        }
        String resData = result.getData().toString();
        JSONObject reJson = JSONObject.parseObject(resData);
        if (reJson.get("status").toString().equals("error")) {
            map.put("code", "-4");
            map.put("info", reJson.get("validateError").toString());
            return map;
        } else {
            map.put("code", "1");
            map.put("info", ExceptionExplain.SUCCESS_BY_UPDATE.getExplain());
        }
        return map;
    }

    @Override
    public Map<String, String> deleteCompanyInfo(String company) {
        Map<String,String> map = new HashMap<String, String>();
        JSONObject keyjson = new JSONObject();
        keyjson.put("COMPANY", company);
//        System.out.println("vform删除上送主键   "+keyjson.toString());
        //vform同步删除
        Result result = vformInterface.delete("D4010", keyjson.toJSONString());
//        System.out.println("vform删除返回结果   "+result);
        if(result==null || !result.getSuccess()) {
            map.put("code", "-3");
            map.put("info", ExceptionExplain.ERROR_BY_VFORM_POST.getExplain());
            return map;
        }
        if(result.getData().toString().equals("{\"message\":\"login\"}")) {
            map.put("code", "-3");
            map.put("info", ExceptionExplain.ERROR_BY_OVERDUE.getExplain());
            return map;
        }
        String resData = result.getData().toString();
        JSONObject reJson = JSONObject.parseObject(resData);
        if(reJson.get("status").toString().equals("error")) {
            map.put("code", "-4");
            map.put("info", reJson.get("validateError").toString());
            return map;
        }else {
            map.put("code", "1");
            map.put("info", ExceptionExplain.SUCCESS_BY_DELETE.getExplain());
        }
        return map;
    }
}
